﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;

namespace VsSpeakerCompanion.UI
{
    internal class LoginControlPresenter : IWidget
    {
        private Context context;
        private LoginControl loginControl;
        private SidebarLoginControl sidebarLoginControl;
        private WidgetManager manager;
        private IUIService uiService;
        private LoginWindow loginWindow;

        internal LoginControlPresenter(Context context, WidgetManager manager, IUIService uiService)
        {
            this.context = context;
            this.manager = manager;
            this.uiService = uiService;

            this.loginControl = new LoginControl();
            this.loginControl.Login += new EventHandler<EventArgs>(OnLoginControlViewLogin);
            this.loginControl.DataContext = this;

            this.sidebarLoginControl = new SidebarLoginControl();
            this.sidebarLoginControl.Login += new EventHandler<EventArgs>(OnSidebarLoginControlViewLogin);

        }

        /// <summary>
        /// Gets or sets the username
        /// </summary>
        public string Username
        {
            get { return this.context.Settings.Get<string>("twitterId"); }
            set { this.context.Settings.Update("twitterId", value); }
        }

        /// <summary>
        /// Gets or sets the session name
        /// </summary>
        public string SessionName
        {
            get { return this.context.Settings.Get<string>("sessionName"); }
            set { this.context.Settings.Update("sessionName", value); }
        }

        /// <summary>
        /// Get or sets the session hashcode
        /// </summary>
        public string SessionHashcode
        {
            get { return this.context.Settings.Get<string>("sessionHashcode"); }
            set { this.context.Settings.Update("sessionHashcode", value); }
        }

        /// <summary>
        /// Gets or sets the conference 
        /// </summary>
        public string ConferenceHashcode
        {
            get { return this.context.Settings.Get<string>("conferenceHashcode"); }
            set { this.context.Settings.Update("conferenceHashcode", value); }
        }

        /// <summary>
        /// See <see cref="IWidget"/> for more information
        /// </summary>
        public void Reset()
        {
        }

        /// <summary>
        /// See <see cref="IWidget"/> for more information
        /// </summary>
        public System.Windows.UIElement VisualElement
        {
            get { return this.sidebarLoginControl; }
        }

        private void OnLoginControlViewLogin(object sender, EventArgs e)
        {
            this.uiService.ExecuteOperation(() =>
                {
                    try
                    {
                        manager.TwitterService.HashCode = this.loginControl.textBoxSession.Text;
                        manager.TwitterService.Login(this.loginControl.textBoxUsername.Text, this.loginControl.textBoxPassword.Password);
                    }
                    catch(Exception ex)
                    {
                        Logger.Log(ex);
                        uiService.ShowError(ex);
                        return;
                    }

                    this.context.StartSession();
                    CloseLoginWindow();
                });
        }

        private void OnSidebarLoginControlViewLogin(object sender, EventArgs e)
        {
            ShowLoginWindow();
        }

        private void ShowLoginWindow()
        {
            if (this.loginWindow != null)
            {
                CloseLoginWindow();
            }

            loginWindow = new LoginWindow();
            loginWindow.content.Children.Add(this.loginControl);

            FrameworkElement relativeTo = this.sidebarLoginControl.startSessionButton;
            Point loginWindowLocation = relativeTo.PointToScreen(relativeTo.TranslatePoint(new Point(0, 0), relativeTo));
            loginWindow.Top = loginWindowLocation.Y - loginWindow.Height / 2 + relativeTo.Height / 2;
            loginWindow.Left = loginWindowLocation.X - loginWindow.Width / 2 + relativeTo.Width / 2;
            if (string.IsNullOrEmpty(loginControl.textBoxPassword.Password) && !string.IsNullOrEmpty(this.loginControl.textBoxUsername.Text))
            {
                loginControl.textBoxPassword.Focus();
            }
            else
            {
                loginControl.textBoxUsername.Focus();
            }

            loginWindow.ShowDialog();
        }

        private void CloseLoginWindow()
        {
            if (this.loginWindow != null)
            {
                this.loginWindow.content.Children.Clear();
                this.loginWindow.CloseWithAnimation();
                this.loginWindow = null;
            }
        }
    }
}